<script setup lang="ts">
import type { FormRules } from 'element-plus'
import { onMounted, reactive, ref } from 'vue'
import { md5 } from 'js-md5'
import { encode } from 'js-base64'
import { rolesAll } from '@/api/interface/web/roles'

interface Props {
  data?: Record<string, any> | never | null
  status?: string | never | null
}

const { data = null, status = 'add' } = defineProps<Props>()

const baseFormRef = ref()

const form = ref<Record<string, any>>({})

const rules = reactive<FormRules>({
  nickname: [{ required: true, message: '昵称不能为空!', trigger: 'blur' }],
  username: [{ required: true, message: '用户名不能为空!', trigger: 'blur' }],
  password: [{ required: true, message: '密码不能为空!', trigger: 'blur' }],
  roleid: [{ required: true, message: '角色不能为空!', trigger: 'blur' }],
})

const rolesOpts = ref<any[]>([])

const getOptions = async () => {
  const res: any = await rolesAll({})
  rolesOpts.value = res.map((item: any) => {
    return {
      label: item.name,
      value: item.id,
    }
  })
}
getOptions()

const initForm = () => {
  if (data) {
    Object.keys(data).forEach((key: string) => {
      if (key == 'roles') {
        form.value.roleid = data![key][0].id
      } else {
        form.value[key] = data![key]
      }
    })
  }
}

const formatForm = () => {
  const result: Record<string, any> = {}
  Object.keys(form.value).forEach((key: string) => {
    if (key === 'password') {
      result[key] = encode(md5(form.value[key]))
    } else {
      result[key] = form.value[key]
    }
  })
  return result
}

const submitForm = () => {
  return baseFormRef.value.submit(formatForm)
}

defineExpose({
  submitForm,
})

onMounted(() => {
  initForm()
})
</script>

<template>
  <BaseForm
    ref="baseFormRef"
    :form="form"
    :rules="rules"
    width="320px"
    :attributes="{ 'label-width': '100px' }"
  >
    <el-form-item label="昵称:" prop="nickname">
      <el-input
        v-model="form.nickname"
        type="text"
        placeholder="请输入内容"
        maxlength="20"
        show-word-limit
      >
      </el-input>
    </el-form-item>
    <el-form-item label="用户名:" prop="username">
      <el-input
        v-model="form.username"
        type="text"
        placeholder="请输入内容"
        maxlength="20"
        show-word-limit
      >
      </el-input>
    </el-form-item>
    <el-form-item v-if="status == 'add'" label="密码:" prop="password">
      <el-input
        v-model="form.password"
        type="text"
        placeholder="请输入内容"
        maxlength="20"
        show-word-limit
        show-password
      >
      </el-input>
    </el-form-item>
    <el-form-item label="角色:" prop="roleid">
      <el-select v-model="form.roleid" placeholder="角色" style="width: 100%">
        <el-option
          v-for="item in rolesOpts"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        />
      </el-select>
    </el-form-item>
    <el-form-item label="状态:" prop="status">
      <el-switch v-model="form.status" active-value="1" inactive-value="0" />
    </el-form-item>
  </BaseForm>
</template>

<style lang="scss" scoped></style>
